import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Login.vue')
// import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Welcome.vue')
// import Welcome from '../components/Welcome.vue'
const Users = () => import(/* webpackChunkName:"users_rights_roles" */ '../components/User/Users.vue')
// import Users from '../components/User/Users.vue'
const Rights = () => import(/* webpackChunkName:"users_rights_roles" */ '../components/Power/Rights.vue')
// import Rights from '../components/Power/Rights.vue'
const Roles = () => import(/* webpackChunkName:"users_rights_roles" */ '../components/Power/Roles.vue')
// import Roles from '../components/Power/Roles.vue'
const Cate = () => import(/* webpackChunkName:"cate_params" */ '../components/Goods/Cate.vue')
// import Cate from '../components/Goods/Cate.vue'
const Params = () => import(/* webpackChunkName:"cate_params" */ '../components/Goods/Params.vue')
// import Params from '../components/Goods/Params.vue'
const GoodsList = () => import(/* webpackChunkName:"goodslist_add" */ '../components/Goods/List.vue')
// import GoodsList from '../components/Goods/List.vue'
const Add = () => import(/* webpackChunkName:"goodslist_add" */ '../components/Goods/Add.vue')
// import Add from '../components/Goods/Add.vue'
const Order = () => import(/* webpackChunkName:"order_report" */ '../components/Order/Order.vue')
// import Order from '../components/Order/Order.vue'
const Report = () => import(/* webpackChunkName:"order_report" */ '../components/Report/Report.vue')
// import Report from '../components/Report/Report.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/Login'   //重定向
  },
  {
    path: '/Login',
    component: Login
  },
  {
    path: '/Home',
    component: Home,
    redirect: '/Welcome',   //重定向到Welcome中
    children: [
      {
        path: '/Welcome',
        component: Welcome
      },
      {
        path: '/Users',
        component: Users
      },
      {
        path: '/Rights',
        component: Rights
      },
      {
        path: '/Roles',
        component: Roles
      },
      {
        path: '/Categories',
        component: Cate
      },
      {
        path: '/Params',
        component: Params
      },
      {
        path: '/Goods',
        component: GoodsList
      },
      {
        path: '/Goods/Add',
        component: Add
      },
      {
        path: '/Orders',
        component: Order
      },
      {
        path: '/Reports',
        component: Report
      },
    ]
  },

]

const router = new VueRouter({
  routes
});
//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to 将要访问路径
  //from代表从哪个路径跳转而来
  //next是一个函数，表示放行
  // next() 放行 next('/login') 强制跳转
  if (to.path == '/Login') return next();  //如果是访问login页面直接放行
  //获取token
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) return next('/Login');//如果没有token 强制跳转login页面
  next();
})
export default router
